<?php
    include('ip2locationlite.class.php');
    include('conn.php');
    //Load the class
    $ipLite = new ip2location_lite;
    $ipLite->setKey('dce2feec54466f534f674db122c7217b1dbbed1213b859cce6a21f8abc5d414c');
     
    //Get errors and locations
    //$_SERVER['REMOTE_ADDR'] = '130.207.0.156';
    $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
    $errors = $ipLite->getError();
     
    //Getting the result
    if (!empty($locations) && is_array($locations)) {
        
        $query = "SELECT max(id) id FROM login_records";
        $rs = pg_query($con, $query) or die("Could not excute query");
        $row = pg_fetch_assoc($rs);
        $id = $row['id'] + 1;
        
        pg_prepare($con, 'login', 'INSERT INTO login_records (username, country, state, city, date, ip, id) VALUES ($1, $2, $3, $4, $5, $6, $7)');
        pg_execute($con, 'login', array($_SESSION['username'], $locations['countryName'], $locations['regionName'], $locations['cityName'], date('Y-m-d'), $_SERVER['REMOTE_ADDR'], $id));
        pg_close($con);
    }
     
    //Show errors
    if (!empty($errors) && is_array($errors)) {
        foreach ($errors as $error) {
            echo var_dump($error);
        }
    }
?>